
/***  DOCUMENTATION LAYER

	Javascript Request File Class

	Last Update: Jul 2006
	Author: Tom at klenwell@gmail.com
	License: GPL License

FUNCTIONS	
	RequestFileLite(debug)	*constructor*
	this.include_file = function(url)
	this.load_file = function(url)
	build_query_string = function(PARAM)
	process_request = function()
	
PROPERTIES
	debug
	url
	query_string
	base_url
	_GET

NOTES
	To link: <script type="text/javascript" src="path/to/file.js"></script>

REFERENCE
	http://developer.mozilla.org/en/docs/JavaScript
	LGPL License: http://www.opensource.org/licenses/lgpl-license.php
	GPL License: http://www.opensource.org/licenses/gpl-license.php

______________________________________________________________________________*/


// class constructor
/*____________________________________________________________________________*/
function RequestFileLite(debug)
{
	this.debug = ( debug == null ) ? 0 : debug;
	this.url = window.location;
	this.query_string = window.location.search;
	this.base_url = this.url.toString().substring(0, this.url.toString().indexOf('?'));
	this._GET = new Object();
	this.file_requested = false;
	
	// clean query string
	if ( this.query_string.substring(0, 1) == '?' ) this.query_string = this.query_string.substring(1);
	
	// method : include_file
	this.include_file = function(url)
	{
		var tag1 = '<scr' + 'ipt type="text/javascript" src="';
		var tag2 = '"></scr' + 'ipt>';
		document.write(tag1 + url + tag2);
	};
	
	// method : load_file
	this.load_file = function(url)
	{
		window.location = url;
	}
	
}; // end cx
/*____________________________________________________________________________*/


// build_query_string
/*____________________________________________________________________________*/
RequestFileLite.prototype.build_query_string = function(PARAM)
{
	var query = '';
	var param_str = '';

	for ( var key in PARAM )
	{
		if ( typeof PARAM[key] == 'function' || typeof PARAM[key] == 'object' ) continue;
		param_str += escape(key) + '=' + escape(PARAM[key]) + '&';
	}

	param_str = param_str.substring(0, param_str.lastIndexOf('&'));
	query = '?' + param_str;

	return query;
};
/*____________________________________________________________________________*/


// process_request
/*____________________________________________________________________________*/
RequestFileLite.prototype.process_request = function()
{
	// declarations
	var request = window.location.search;		// query string
	
	// was there a request
	if ( !request )
	{
		if ( this.debug == 1 ) alert('no request');
		this.file_requested = false;
		return;
	}
	
	// parse request
	if ( request.substring(0, 1) == '?' ) request = request.substring(1);
	
	// parse request
	request = unescape(request); 
	var GET_PARAM = request.split('&');
  for ( var i = 0; (i < GET_PARAM.length); i++) 
	{
		GET_PAIR = GET_PARAM[i].split('=');
		if ( this.debug == 1 ) alert(GET_PAIR[0] + ' : ' + GET_PAIR[1]);
		
		// null key
		if ( GET_PAIR[1] == null ) 
		{
			GET_PAIR[1] = GET_PAIR[0];
			GET_PAIR[0] = null;
		}
		
		// get key
		_gkey = ( GET_PAIR[0] != null ) ? GET_PAIR[0] : i;
		
		// get value
		_gval = GET_PAIR[1];
		if ( this.debug == 1 ) alert(_gkey + ' : ' + _gval);
		
		this._GET[_gkey] = _gval;
  }
	
	if ( GET_PARAM.length > 1 ) this.file_requested = true;
	
	return this._GET;	
};
/*____________________________________________________________________________*/

